package com.njust.controller;

import com.github.pagehelper.Page;
import com.njust.domain.dto.EquipmentDTO;
import com.njust.domain.dto.EquipmentPageQueryDTO;
import com.njust.domain.dto.RequestDTO;
import com.njust.domain.dto.SolutionDTO;
import com.njust.domain.vo.PageQueryVO;
import com.njust.result.Result;
import com.njust.service.EquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: Grtiop
 * @Description: 设备相关
 * @DateTime: 2024/11/15 下午2:10
 **/

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/equipment")
@Slf4j
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;

  /**
  * @Description  根据项目id分页查询有关设备
  * @param equipmentPageQueryDTO
  * @return com.njust.result.Result<com.njust.domain.vo.PageQueryVO>
  * @Author Grtiop
  * @Date 2024/11/15
  */


    @GetMapping("/pageQuery_equipment")
    public Result<PageQueryVO> pageQuery(EquipmentPageQueryDTO equipmentPageQueryDTO){
        log.info("设备分页查询，参数为:{}",equipmentPageQueryDTO);
        PageQueryVO pageQueryVO = equipmentService.pageQuery(equipmentPageQueryDTO);
        return Result.success(pageQueryVO);
    }

    /**
    * @Description  添加设备
    * @param equipmentDTO
    * @return com.njust.result.Result<java.lang.String>
    * @Author Grtiop
    * @Date 2024/11/15
    */

    @PostMapping("/add_equipment")
    public Result<String> addEquipment(@RequestBody EquipmentDTO equipmentDTO){
        log.info("添加设备, {}",equipmentDTO);
        int result = equipmentService.addEquipment(equipmentDTO);
        if(result >= 1) return Result.success("设备添加成功");
        else return Result.error("设备添加失败");
    }

    /**
    * @Description  添加解决方案
    * @param solutionDTO
    * @return com.njust.result.Result<java.lang.String>
    * @Author Grtiop
    * @Date 2024/11/15
    */

    @PostMapping("/add_solution")
    public Result<String> addSolution(@RequestBody SolutionDTO solutionDTO){
        log.info("添加解决方案,{}",solutionDTO);
        int result = equipmentService.addSolution(solutionDTO);
        if(result >= 1) return Result.success("解决方案添加成功");
        else return Result.error("解决方案添加失败");
    }

    /**
    * @Description  修改解决方案
    * @param solutionDTO
    * @return com.njust.result.Result<java.lang.String>
    * @Author Grtiop
    * @Date 2024/11/15
    */

    @PutMapping("/update_solution")
    public Result<String> updateSolution(@RequestBody SolutionDTO solutionDTO){
        log.info("修改解决方案,{}",solutionDTO);
        int result = equipmentService.updateSolution(solutionDTO);
        if(result >= 1) return Result.success("解决方案修改成功");
        else return Result.error("解决方案修改失败");
    }

    /**
    * @Description  查询解决方案
    * @param equipment_id
    * @return com.njust.result.Result<com.njust.domain.vo.PageQueryVO>
    * @Author Grtiop
    * @Date 2024/11/15
    */

    @GetMapping("/getSolution/{equipment_id}")
    public Result<PageQueryVO> getSolution(@PathVariable int equipment_id){
        log.info("解决方案查询，参数为:{}",equipment_id);
        PageQueryVO pageQueryVO = equipmentService.searchSolution(equipment_id);
        return Result.success(pageQueryVO);
    }

    /**
    * @Description
    * @param solution_id
    * @return com.njust.result.Result<java.lang.String>
    * @Author Grtiop
    * @Date 2024/11/15
    */

    @DeleteMapping("/delete_solution")
    public Result<String> deleteSolution(int solution_id){
        log.info("删除解决方案,id为: {}",solution_id);
        int result = equipmentService.deleteSolution(solution_id);
        if(result >= 1) return Result.success("解决方案删除成功");
        else return Result.error("解决方案删除失败");
    }

    /**
    * @Description
    * @param requestDTO
    * @return com.njust.result.Result<java.lang.String>
    * @Author Grtiop
    * @Date 2024/11/15
    */

    @PostMapping("/add_request")
    public Result<String> addRequest(@RequestBody RequestDTO requestDTO){
        log.info("申请采购,参数为:{}",requestDTO);
        int result = equipmentService.addRequest(requestDTO);
        if(result >= 1) return Result.success("申请成功");
        else return Result.error("申请失败");
    }

    /**
    * @Description
    * @param equipment_procurement_id
    * @return com.njust.result.Result<java.lang.String>
    * @Author Grtiop
    * @Date 2024/11/16
    */

    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@RequestBody int equipment_procurement_id){
        log.info("采购状态变更,id为:{}",equipment_procurement_id);
        int result = equipmentService.updateStatus(equipment_procurement_id);
        if(result >= 1) return Result.success("采购状态变更成功");
        else return Result.error("采购状态变更失败");
    }

    /**
    * @Description
    * @param page
    * @param pageSize
    * @return com.njust.result.Result<com.njust.domain.vo.PageQueryVO>
    * @Author Grtiop
    * @Date 2024/11/16
    */

    @GetMapping("/pageQuery_procurement")
    public Result<PageQueryVO> pageQueryProcurement(int page, int pageSize){
        log.info("分页查询采购申请,页码为:{},每页大小为:{}",page,pageSize);
        PageQueryVO pageQueryVO = equipmentService.pageQueryProcurement(page,pageSize);
        return Result.success(pageQueryVO);
    }

    /**
    * @Description
    * @param project_id
    * @return void
    * @Author Grtiop
    * @Date 2024/11/16
    */

    @GetMapping("/export_procurement/{project_id}")
    public void exportProcurement( HttpServletResponse response,@PathVariable int project_id){
        log.info("导出采购申请,项目id为:{}",project_id);
        equipmentService.exportExcel(project_id,response);
    }
}
